﻿// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include "rsahelper/rsahelper.h"

#include <DConfig>

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDBusInterface>

DCORE_USE_NAMESPACE

class NetClientJob : public QObject
{
    Q_OBJECT
public:
    explicit NetClientJob(QObject *parent = nullptr);
    ~NetClientJob();

    void quit();

    enum InterfaceType {
        None,
        RegisterClient,
        GetQr,
        CheckQr,
        QueryLocalUserInfo,
        SaveLocalUserInfo,
        CheckFace,
    };
    enum QrStatus {
        AcceptLogin = 1, // 用户同意登录
        AuthFailed, // 认证失败
        NotScaned, // 还未扫码
        ScandedNotAccept, // 已经扫码，但还未同意
        Expired, // 二维码过期
    };

    QString getIdTocken();

Q_SIGNALS:
    void registerClientFinished(bool successed, const QString &clientCode);
    void getQrCodeFinished(bool successed, const QString &qr);
    void checkQrCodeFinished(bool successed, QrStatus status);
    void queryLocalUserInfoFinished(bool successed, const QString &localUserName);
    void saveLocalUserInfoFinished(bool successed, const QString &localUserName);
    void checkFaceFinished(bool successed);

    void checkHttpSrvValidityFinished(bool valid);

public Q_SLOTS:
    void onRequestRegisterClient(const QString &clientCode);
    void onRequestGetQrCode();
    void onRequestCheckQrCode();
    void onRequestQueryLocalUserInfo();
    void onRequestSaveLocalUserInfo(const QString &localUserName,
                                    const QString &localUserFullName,
                                    const QString &localUserPasswd);
    void onRequestCheckFace(const QString &imgData);

    void onRequestTryCreateLocalUser();
    void onRequestCheckHttpSrvValidity();
    void onRequestStopHttpReply();

private Q_SLOTS:
    void onHttpDataDownloadFinished();

private:
    void parseDataOfRegisterClient(const QByteArray &content);
    void parseDataOfGetQr(const QByteArray &content);
    void parseDataOfCheckQr(const QByteArray &content);
    void parseDataOfQueryLocalUserInfo(const QByteArray &content);
    void parseDataOfSaveLocalUserInfo(const QByteArray &content);
    void parseDataOfCheckFace(const QByteArray &content);

    bool isLocalUserExist(const QString &localUserName);
    bool createLocalUser(const QString &localUserName, const QString &fullName, const QString &passwd);

    void asynCheckHttpSrvValidity();
    void stopHttpReply();

private:
    QString m_httpAddress;
    RsaHelper *m_rsaHelper;
    QNetworkAccessManager *m_networkAccessManager;
    QNetworkReply *m_checkHttpSrvValidityNetReply;
    InterfaceType m_currentIfcType;
    QNetworkReply *m_netReply;

    QString m_clientCode; // 客户端编码(客户端的唯一标识)
    QString m_qrKey;
    QrStatus m_qrStatus;
    QString m_userId;
    QString m_userDisplayName;
    QString m_userAdviceName;
    QString m_userIdToken;
    QString m_localUserName;
    QDBusInterface *m_accoutDbusIfc;
};
